package com.labot.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.AccessType;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;

import com.labot.exceptions.ExceptionDomain;

/**
 * 
 * @author Ing. Enrique Barbeito
 * 
 */
@Entity
@Table(name = "app_messages")
@AccessType("field")
@Indexed
@XmlRootElement
public class I18n extends BaseObject  implements Serializable {
    
	private static final long serialVersionUID = 3274417485823628731L;
    
	@Field
	@Column(name = "key_message", nullable = false, length = 255, unique = true)
    private String key;
    
    @Field
    @Column(length = 1024)
    private String en = "";

    @Field
    @Column(length = 1024)
    private String es = "";

    /**
     * Default constructor - creates a new instance with no values set.
     */
    @SuppressWarnings("unused")
    private I18n() {
    }
    
    public I18n(String key) {
    	this.setKey(key);
    	this.en = "???" + key + "???";
		this.es = "???" + key + "???";
    }

	public String getKey() {
		return key;
	}


	public void setKey(String key) throws ExceptionDomain {
		if ((key == null)||(key.trim().equals(""))) {
			throw new ExceptionDomain("Not Allowd empty key!");
		}
		this.key = key;
		
	}


	public String getEn() {
		if(en == null) {
			return "";
		}
		return en;
	}


	public void setEn(String en) {
		if((en == null)||(en.trim().equals(""))) {
			this.en = "???" + key + "???";
		} else {			
			this.en = en;
		}
	}


	public String getEs() {
		if(es == null) {
			return "";
		}
		return es;
	}


	public void setEs(String es) {
		if((es == null)||(es.trim().equals(""))) {
			this.es = "???" + key + "???";
		} else {
			this.es = es;
		}			
	}

	/**
     * {@inheritDoc}
     */
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof I18n)) {
            return false;
        }

        final I18n config = (I18n) o;

        return (config.getKey().equals(this.getKey()));

    }

    /**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
        		.append("id", this.getId())
                .append("key", this.getKey())
                .append("en", this.getEn())
                .append("es", this.getEs());
        return sb.toString();
    }
}
